package com.xinchuang.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xinchuang.comment.R;
import com.xinchuang.domain.vo.PartResVo;
import com.xinchuang.entity.Part;
import com.xinchuang.entity.PartMoldType;
import com.xinchuang.mapper.PartMapper;
import com.xinchuang.mapper.PartMoldTypeMapper;
import com.xinchuang.service.IPartService;
import com.xinchuang.service.PartModService;
import com.xinchuang.utils.HanyuPinyinUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class PartModServiceImpl implements PartModService {

    @Autowired
    private IPartService partService;

    @Autowired
    private PartMapper partMapper;

    @Autowired
    private PartMoldTypeMapper partMoldTypeMapper;

    private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @Override
    public String getPartPage(JSONObject jsonObject) {
        //jsonobject解析获取页码
        String pageNum = jsonObject.getString("pageNum");
        //jsonobject解析获取零件名称
        String partName = jsonObject.getString("partName");
        //jsonobject解析获取分页数量
        String pageSize = jsonObject.getString("pageSize");
        LambdaQueryWrapper<Part> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.like(StringUtils.isNotEmpty(partName), Part::getPartName, partName);
        queryWrapper.orderByDesc(Part::getCreateTime);
        Page<Part> partPage = partMapper.selectPage(new Page<>(Integer.valueOf(pageNum), Integer.valueOf(pageSize)), queryWrapper);
        JSONObject userInfoJson = new JSONObject();
        userInfoJson.put("dataList", partPage.getRecords());
        userInfoJson.put("totalNum", partPage.getTotal());
        return JSON.toJSONString(R.data(userInfoJson));
    }

    @Override
    public String savePart(JSONObject jsonObject) {
        //jsonobject解析获取单价
        String price = jsonObject.getString("price");
        //jsonobject解析获取密度
        String density = jsonObject.getString("density");
        //jsonobject解析获取是否为模架
        Boolean isMoldFrame = jsonObject.getBoolean("isMoldFrame");
        //jsonobject解析获取零件名称
        String partName = jsonObject.getString("partName");
        //jsonobject解析获取是否必选
        Boolean isRequired = jsonObject.getBoolean("isRequired");
        //jsonobject解析获取是否为主材
        Boolean isMainMaterial = jsonObject.getBoolean("isMainMaterial");
        //jsonobject解析获取是否为是标准件
        Boolean isStandardCom = jsonObject.getBoolean("isStandardCom");
        //jsonobject解析获取备用参数1
        String param1 = jsonObject.getString("param1");
        //jsonobject解析获取备用参数2
        String param2 = jsonObject.getString("param2");
        //jsonobject解析获取备用参数3
        String param3 = jsonObject.getString("param3");
        //jsonobject解析获取备用参数4
        String param4 = jsonObject.getString("param4");
        //jsonobject解析获取备用参数5
        String param5 = jsonObject.getString("param5");
        //jsonobject解析获取备用参数6
        String param6 = jsonObject.getString("param6");
        //jsonobject解析获取备用参数7
        String param7 = jsonObject.getString("param7");
        //jsonobject解析获取备用参数8
        String param8 = jsonObject.getString("param8");
        //jsonobject解析获取备用参数9
        String param9 = jsonObject.getString("param9");
        //jsonobject解析获取零件Id
        String partId = jsonObject.getString("partId");
        if (StringUtils.isEmpty(partId)) {
            List<Part> parts = partMapper.selectList(new LambdaQueryWrapper<Part>().eq(Part::getPartName, partName));
            if (parts.size() > 0) {
                return JSON.toJSONString(R.fail("零件名称已存在，请重新输入"));
            }
        }
        try {
            Double.parseDouble(price);
        } catch (NumberFormatException e) {
            return JSON.toJSONString(R.fail("价格必须为数字"));
        }
        try {
            Double.parseDouble(density);
        } catch (NumberFormatException e) {
            return JSON.toJSONString(R.fail("密度必须为数字"));
        }
        Part part = JSON.toJavaObject(jsonObject, Part.class);
        part.setCreateTime(LocalDateTime.now());
        boolean b = partService.saveOrUpdate(part);
        if (b) {
            PartMoldType partMoldType = new PartMoldType();
            partMoldType.setPartName(partName);
            partMoldType.setPrice(price);
            partMoldType.setDensity(density);
            partMoldType.setIsMainMaterial(isMainMaterial);
            partMoldType.setIsMoldFrame(isMoldFrame);
            partMoldType.setIsRequired(isRequired);
            partMoldType.setIsStandardCom(isStandardCom);
            partMoldTypeMapper.update(partMoldType, new LambdaQueryWrapper<PartMoldType>().eq(PartMoldType::getPartName, partName));
            return JSON.toJSONString(R.success("保存成功"));
        }
        return JSON.toJSONString(R.fail("保存失败"));

    }

    @Override
    public String getPartDetail(JSONObject jsonObject) {
        //jsonobject解析获取零件Id
        String partId = jsonObject.getString("partId");
        Part part = partMapper.selectById(partId);
        JSONObject userInfoJson = new JSONObject();
        userInfoJson.put("partName", part.getPartName());
        userInfoJson.put("partId", part.getPartId());
        userInfoJson.put("isMoldFrame", part.getIsMoldFrame());
        userInfoJson.put("density", part.getDensity());
        userInfoJson.put("price", part.getPrice());
        userInfoJson.put("isRequired", part.getIsRequired());
        return JSON.toJSONString(R.data(userInfoJson));
    }

    @Override
    public String getPartList() {
        List<Part> partList = partMapper.selectList(null);
        List<PartResVo> partResVoList = partList.stream().map(part -> {
            PartResVo partResVo = new PartResVo();
            BeanUtils.copyProperties(part, partResVo);
            partResVo.setPartNamePinYin(HanyuPinyinUtil.chineseToPinyin(part.getPartName()));
            return partResVo;
        }).collect(Collectors.toList());
        JSONObject userInfoJson = new JSONObject();
        userInfoJson.put("dataList", partResVoList);
        return JSON.toJSONString(R.data(userInfoJson));
    }

    @Override
    public String deletePart(JSONObject jsonObject) {
        //jsonobject解析获取ID列表
        JSONArray idList = jsonObject.getJSONArray("idList");
        int size = idList.size();
        if (size > 0) {
            int i = partMapper.deleteBatchIds(idList);
            if (i == size) {
                return JSON.toJSONString(R.success("删除成功"));
            }
        }
        return JSON.toJSONString(R.fail("删除失败"));

    }

}
